﻿#include "EditorObjectPropertyAbilityView.h"

#include "Widgets/Positioners/VerticalPositionerWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/ImageWidget.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Property/EditorPropertyView.h"
#include "Property/EditorPropertyViewModel.h"


void REditorObjectPropertyAbilityView::BindViewModel(TSharedObjectPtr<REditorAbilityViewModel> InViewModel)
{
    Super::BindViewModel(InViewModel);
    PropertyViewModel = InViewModel;

    PropertyViewModel->OnPropertyListChanged.Bind([this](){
        UpdateWidget();
    });
}

void REditorObjectPropertyAbilityView::CreateWidget(TSharedObjectPtr<RWidget> InParent)
{
    auto RootWidget=NewWidget<RWidget>(InParent);
    RootWidget->Anchor.Fill(InParent);

    auto NoObject=NewWidget<RTextWidget>(RootWidget);
    NoObject->Text=U"未选中对象,请选中对象以查看属性";
    NoObject->Anchor.Fill(RootWidget);
    NoObject->HorizontalAlignment=ETextHorizontalAlignment::Center;
    NoObject->VerticalAlignment=ETextVerticalAlignment::Center;
    NoObject->Visible=[this](){
        return !PropertyViewModel->HasObject();
    };

    auto NoProperty=NewWidget<RTextWidget>(RootWidget);
    NoProperty->Text=U"当前对象没有可以展示的属性";
    NoProperty->Anchor.Fill(RootWidget);
    NoProperty->HorizontalAlignment=ETextHorizontalAlignment::Center;
    NoProperty->VerticalAlignment=ETextVerticalAlignment::Center;
    NoProperty->Visible=[this](){
        return PropertyViewModel->HasObject() && PropertyViewModel->GetPropertiesNum()==0;
    };

    List=NewWidget<RWidget>(InParent);
    List->Anchor.Fill(InParent);
    List->Visible=[this](){
        return PropertyViewModel->GetPropertiesNum()>0;
    };

    UpdateWidget(); 

}

void REditorObjectPropertyAbilityView::FillOperations(TSharedObjectPtr<RWidget> InTitleBar)
{
    //do not have operation for now
}

void REditorObjectPropertyAbilityView::UpdateWidget()
{
    //update property views
    Properties.Clear();
    for(int64_t i=0;i<PropertyViewModel->GetPropertiesNum();i++)
    {
        CClass* PropertyViewClass=REditor::Get()->GetViewClass(PropertyViewModel->GetProperty(i)->GetClass());
        auto PropertyView=NewObject<REditorPropertyView>(PropertyViewClass,this);
        PropertyView->BindViewModel(PropertyViewModel->GetProperty(i));
        Properties.Add(PropertyView);
    }


    //update widget
    List->ClearSubWidgets();

    auto ValueContainer=NewWidget<RVerticalPositionerWidget>(List);
    ValueContainer->Anchor.Fill(List);

    for(int64_t i=0;i<Properties.Num();i++)
    {
        auto Property=Properties[i];
        Property->CreateWidget(ValueContainer);
    }

}
